import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';
import '../../widgets/someCards.dart';
import '../../model/inspectreport.dart';

typedef InspectAddCallback = void Function(InspectReport r1);

class InspectReportListPage extends StatefulWidget {
  @override
  InspectReportListPageState createState() => new InspectReportListPageState();
}

class InspectReportListPageState extends State<InspectReportListPage> {
  List<InspectReport> RList = <InspectReport>[];
  bool isEnd = false;
  ScrollController _scrollController = new ScrollController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        getMore();
      }
    });

    getRefrush();
  }

  Future<Null> getMore() async {
    // await addOne();
    int count = 10;
    if (isEnd == true) return;
    int page = RList.length ~/ count;
    var list1 = await InspectReport.GetList(1, 10);
    list1.removeWhere((p) => RList.any((x) => x.ID == p.ID));
    setState(() {
      if (list1.length < count) isEnd = true;
      RList.insertAll(RList.length, list1);
    });
  }

  onAdded(InspectReport r1) => {
        setState(() {
          RList.insert(0, r1);
        })
      };

  String getQueryStr() {
    if (UIData.CurrentUser!.Authority == "管理员") {
      return " State>=0";
    }
    return "UserId=@ID and State>=0";
  }

  Future<Null> getNewer() async {
    var list1 = await InspectReport.GetList(1, 10);
    if (list1.length == 0) return;

    var list2 =
        list1.where((p) => RList.any((x) => x.ID == p.ID) == false).toList();
    if (list2.length == 0) return;

    list2.insertAll(list2.length, RList);

    setState(() {
      RList = list2;
    });
  }

  Future<Null> getRefrush() async {
    var list1 = await InspectReport.GetList(1, 10);
    setState(() {
      RList = list1;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (RList == null) return new Container();
    // TODO: implement build
    return new Scaffold(
        appBar: AppBar(
          title: Text("库区巡查"),
        ),
        floatingActionButton: Container(
            margin: EdgeInsets.all(16.0),
            child: FloatingActionButton(
              child: Text(
                "+",
                style: TextStyle(fontSize: 24.0),
              ),
              onPressed: () => RouteHelper.InspectAdd(context, () => {}),
            )),
        body: Container(
            margin: EdgeInsets.only(top: 8.0),
            child: new RefreshIndicator(
              child: ListView.builder(
                itemCount: RList.length + 1,
                controller: _scrollController,
                physics: const AlwaysScrollableScrollPhysics(),
                itemBuilder: (BuildContext context, int index) {
                  if (index != RList.length) {
                    return SomeCard.InspectReportCard(
                        context, RList[index], getRefrush);
                  } else {
                    if (isEnd == true) {
                      return Container(
                          margin: EdgeInsets.all(12.0),
                          child: Center(
                              child: Text(
                            "没有更多了",
                            style: TextStyle(color: Colors.grey),
                          )));
                    } else {
                      return new Container();
                    }
                  }
                },
              ),
              onRefresh: getNewer,
            )));
  }
}
